package com.example.liumuyao.recyclerviewtest_412lmy.model;

import com.example.liumuyao.recyclerviewtest_412lmy.bean.tcaseBean;
import com.example.liumuyao.recyclerviewtest_412lmy.iface.tcaseListener;
import com.example.liumuyao.recyclerviewtest_412lmy.iface.tcaseiface;
import com.example.liumuyao.recyclerviewtest_412lmy.service.tcaseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Liu Muyao on 2017/4/23.
 */

public class tcaseModel implements tcaseiface {
    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";
    //构造方法
    public tcaseModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    @Override
    public void getResultList(String mod, int page, String sessionID, final tcaseListener listener) {
        tcaseService tcaseService = retrofit.create(tcaseService.class);
        Call<List<tcaseBean>> call= tcaseService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<tcaseBean>>() {
            @Override
            public void onResponse(retrofit2.Call<List<tcaseBean>> call, Response<List<tcaseBean>> response) {
                if (response.isSuccessful() && response.body()!=null){
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("on response fail");
                }
            }

            @Override
            public void onFailure(retrofit2.Call<List<tcaseBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
